package com.example.bilibili_miaosha.controller;


import com.example.bilibili_miaosha.entity.User;
import com.example.bilibili_miaosha.rabbitmq.MQSender;
import com.example.bilibili_miaosha.service.IUserService;
import com.example.bilibili_miaosha.vo.RespBean;
import com.wf.captcha.SpecCaptcha;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * 弓川hello mybatisplus
 * </p>
 *
 * @author gongchuan
 * @since 2022-03-27
 */
@Slf4j
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private MQSender mqSender;
    @Autowired
    private RedisTemplate redisTemplate;
    /** 
    * @Description: 查看用户信息（测试） 
    * @Param:  
    * @return:  
    * @Author: 弓川
    * @Date: 2022/4/9 
    **/ 
    @RequestMapping("/info")
    @ResponseBody
    public RespBean info(User user){
//        User user = userService.getUserByCookie(userTicket);
        return RespBean.success(user);
    }

    /**
     * 测试发送RabbitMQ消息
     */
    @RequestMapping("/mq")
    @ResponseBody
    public void mq() {
        mqSender.send("Hello");
    }

    @RequestMapping(name = "/captcha",method = RequestMethod.GET)
    public void verifyCode(HttpServletRequest request, HttpServletResponse response)  {
        System.out.println("验证码被调用了");

        // 设置请求头为输出图片类型
        response.setContentType("image/jpg");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        // 三个参数分别为宽、高、位数
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 32, 5);

        // 验证码存入session
//        request.getSession().setAttribute("captcha", specCaptcha.text().toLowerCase());
        System.out.println("验证码："+specCaptcha.text().toLowerCase());
        //验证码存到了redis
        // 输出图片流
        try {
            specCaptcha.out(response.getOutputStream());
        }catch (IOException e) {
            log.error("验证码生成失败");  //@sl4j才可以用的
        }
    }


}
